package com.hzz.web.app.sys;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hzz.framework.Response;
import com.hzz.framework.annotation.NoAuthorize;
import com.hzz.framework.util.WechatUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

@Api(tags = "公众号")
@RestController
@Slf4j
@NoAuthorize
public class WechatController {
	static final String acPrefix = "/api/wechat/";

	@Autowired
	private WxMpService wxMpService;

	/**
	 * 方法描述：主方法，用于微信认证
	 */
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = acPrefix + "auth")
	public void main(HttpServletResponse response, String signature, String timestamp, String nonce, String echostr) {
		log.info("WechatController.auth begin>>>{},{},{},{}", signature, timestamp, nonce, echostr);
		boolean flag = wxMpService.checkSignature(timestamp, nonce, signature);
		log.info("WechatMain.auth end>>>{}", flag);
		if (flag) {
			PrintWriter o = null;
			try {
				o = new PrintWriter(response.getWriter());
				o.print(echostr);
			} catch (IOException e) {
				log.error("写回微信端错误{}", e.getMessage());
			} finally {
				o.close();
			}
		}
	}

	/**
	 * 获取公众号二维码图片地址
	 */
	@ApiOperation(value = "获取公众号二维码图片地址")
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = acPrefix + "qrCodePictureUrl")
	public Response<String> qrCodePictureUrl() {
		try {
			WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateLastTicket(1);
			String url = wxMpService.getQrcodeService().qrCodePictureUrl(ticket.getTicket());
			return new Response(url);
		} catch (Exception e) {
			return Response.error(e.getMessage());
		}
	}

	/**
	 * 根据code获取openId
	 */
	@ApiOperation(value = "根据code获取openId")
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = acPrefix + "getOpenIdByCode")
	public Response<String> getOpenIdByCode(String code) {
		log.info("WechatController.getOpenIdByCode begin>>>{}", code);
		try {
			// 根据code获取微信openid
			WxMpUser wxUser = WechatUtil.getWechatInfo(code);
			log.info("WechatController.getOpenIdByCode result>>>{}", wxUser);
			return new Response(wxUser.getOpenId());
		} catch (Exception e) {
			return Response.error(e.getMessage());
		}
	}

	/**
	 * 获取JS-SDK配置
	 */
	@ApiOperation(value = "获取JS-SDK配置")
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = acPrefix + "getJsConfig")
	public Response<WxJsapiSignature> getJsConfig(String url) {
		log.info("WechatController.getJsConfig begin>>>{}", url);
		try {
			WxJsapiSignature signature = wxMpService.createJsapiSignature(url);
			log.info("WechatController.getJsConfig result>>>{}", signature);
			return new Response(signature);
		} catch (Exception e) {
			return Response.error(e.getMessage());
		}
	}

	@ApiOperation(value = "创建菜单")
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = acPrefix + "createMenu")
	public Response<WxJsapiSignature> createMenu() {
		log.info("WechatController.createMenu begin>>>{}");
		try {
			String domain = "https://39.98.203.5:8444/";
			// String domain = "http://47.108.171.242:10011/";
			WxMenu menu = new WxMenu();
			List<WxMenuButton> buttons = new ArrayList<>();
			// WxMenuButton button1 = new WxMenuButton();
			// button1.setName("投诉举报");
			// button1.setType(WxConsts.MenuButtonType.VIEW);
			// // button1.setUrl("http://www.cjeye.net");
			// button1.setUrl(domain + "wechat/complain/add");

			WxMenuButton button2 = new WxMenuButton();
			button2.setName("科普知识");
			button2.setType(WxConsts.MenuButtonType.CLICK);
			List<WxMenuButton> subButtons2 = new ArrayList<>();// 子菜单
			WxMenuButton subBtn2_1 = new WxMenuButton();
			subBtn2_1.setName("河湖档案");
			subBtn2_1.setType(WxConsts.MenuButtonType.VIEW);
			subBtn2_1.setUrl(domain + "wechat/riverList");
			subButtons2.add(subBtn2_1);
			WxMenuButton subBtn2_2 = new WxMenuButton();
			subBtn2_2.setName("知识园地");
			subBtn2_2.setType(WxConsts.MenuButtonType.VIEW);
			subBtn2_2.setUrl(domain + "wechat/noticeList");
			subButtons2.add(subBtn2_2);
			WxMenuButton subBtn2_3 = new WxMenuButton();
			subBtn2_3.setName("宣传引导");
			subBtn2_3.setType(WxConsts.MenuButtonType.VIEW);
			subBtn2_3.setUrl(domain + "wechat/publicityList");
			subButtons2.add(subBtn2_3);
			WxMenuButton subBtn2_4 = new WxMenuButton();
			subBtn2_4.setName("转载文章");
			subBtn2_4.setType(WxConsts.MenuButtonType.VIEW);
			subBtn2_4.setUrl(domain + "wechat/reprintedList");
			subButtons2.add(subBtn2_4);
			button2.setSubButtons(subButtons2);

			WxMenuButton button3 = new WxMenuButton();
			button3.setName("共享信息");
			button3.setType(WxConsts.MenuButtonType.CLICK);
			List<WxMenuButton> subButtons3 = new ArrayList<>();// 子菜单
			WxMenuButton subBtn3_1 = new WxMenuButton();
			subBtn3_1.setName("政策新闻");
			subBtn3_1.setType(WxConsts.MenuButtonType.VIEW);
			subBtn3_1.setUrl(domain + "wechat/policyList");
			subButtons3.add(subBtn3_1);
			WxMenuButton subBtn3_2 = new WxMenuButton();
			subBtn3_2.setName("镇级公众号");
			subBtn3_2.setType(WxConsts.MenuButtonType.VIEW);
			subBtn3_2.setUrl(domain + "wechat/gzhList");
			subButtons3.add(subBtn3_2);
			WxMenuButton subBtn3_3 = new WxMenuButton();
			subBtn3_3.setName("公众号二维码");
			subBtn3_3.setType(WxConsts.MenuButtonType.VIEW);
			// subBtn3_3.setUrl(domain + "wechat/gzhImg");
			subBtn3_3.setUrl(domain + "upload/image/qrcode.jpg");
			subButtons3.add(subBtn3_3);
			WxMenuButton button3_4 = new WxMenuButton();
			button3_4.setName("投诉举报");
			button3_4.setType(WxConsts.MenuButtonType.VIEW);
			button3_4.setUrl(domain + "wechat/complain/add");
			subButtons3.add(subBtn3_3);

			// buttons.add(button1);
			buttons.add(button2);
			buttons.add(button3);
			menu.setButtons(buttons);
			String result = wxMpService.getMenuService().menuCreate(menu);
			log.info("WechatController.createMenu result>>>{}", result);
			return new Response(result);
		} catch (Exception e) {
			return Response.error(e.getMessage());
		}
	}
}
